const states = {
  rolelist: [],
  menulist: []
}

const mutations = {
  SET_ROLELIST: (state, rolelist) => {
    state.rolelist = rolelist
  },
  ADD_ROLE: (state, role) => {
    state.rolelist = state.rolelist.concat(role)
  },
  EDIT_ROLE: (state, role) => {
    state.rolelist = state.rolelist.map((item) => {
      if (item.id === role.id) {
        // eslint-disable-next-line no-param-reassign
        item = role
      }
      return item
    })
  },
  SET_MENULIST: (state, menulist) => {
    state.menulist = menulist
  }
}

const actions = {
  setRolelist({ commit }, rolelist) {
    commit('SET_ROLELIST', rolelist)
  },
  addRole({ commit }, role) {
    commit('ADD_ROLE', role)
  },
  editRole({ commit }, role) {
    commit('EDIT_ROLE', role)
  },
  setMenuList({ commit }, menus) {
    commit('SET_MENULIST', menus)
  }
}

export default {
  namespaced: true,
  state: states,
  mutations,
  actions
}
